
package interrupt;


public class Tools {
    private Tools(){}
    
    public static byte getRegnum(String Reg){
        return Byte.parseByte(Reg.substring(1), 16);
    }
    
    public static byte getOffset(int AR, String ADR){
        int adr = Integer.parseInt(ADR, 16);
        return (byte) (AR - adr);
    }
    
    public static short reverse(Short sourse){
        StringBuilder result = new StringBuilder(Integer.toBinaryString(sourse.intValue()));
        while(result.length() > 16)
            result.delete(0, 1);
        while(result.length() < 16)
        	result = new StringBuilder("0"+result.toString());
        result.reverse();
        return Short.valueOf(result.toString(), 2).shortValue();
    }
    
    public static String getADR(short adr){
        StringBuilder ADR = new StringBuilder(Integer.toHexString(adr));
        while(ADR.length() < 4){
            ADR = new StringBuilder("0"+ADR.toString());
        }
        while(ADR.length() > 4){
            ADR.delete(0, 1);
        }
        return ADR.toString();
    }
    
    public static String align(String sourse){
        StringBuilder result = new StringBuilder(sourse);
        while(result.length() < 4){
            result = new StringBuilder(result.toString()+" ");
        }
        return result.toString();
    }
    
    public static String fill(String sourse){
        StringBuilder result = new StringBuilder(sourse);
        while(result.length() < 4){
            result = new StringBuilder("0"+result.toString());
        }
        return result.toString().toUpperCase();
    }
    
    public static StringBuilder getBin(short sourse){
        StringBuilder result = new StringBuilder(Integer.toBinaryString(sourse));
        while(result.length() < 16)
            result = new StringBuilder("0"+result.toString());
        while(result.length() > 16)
            result.delete(0, 1);
        return result;
    }
}
